package com.reason.chat.controller;

import com.alibaba.fastjson.JSON;
import com.reason.chat.Mapper.FriendMapper;
import com.reason.chat.pojo.Afriend;
import com.reason.chat.pojo.Friend;
import com.reason.chat.pojo.Friendlist;
import com.reason.chat.pojo.Usermine;
import com.reason.chat.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @author xfx
 */
@Controller
@RequestMapping("/chats")
public class ChatController {


    @Autowired
    ChatService chatService;
    @Autowired
    WebSocketController webSocketController;

    @RequestMapping("/Chatinterface")
    public String Chatinterface(Model model, HttpServletRequest request) {
        String idStr = this.getCookie("id", request);
        Usermine usermine = chatService.getUsermine(Long.valueOf(idStr));
        List<Friendlist> friendlist = chatService.getFriendlist(Long.valueOf(idStr));
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("10001");
        System.out.println(JSON.toJSON(friendlist));
        model.addAttribute("mine", JSON.toJSON(usermine));
        model.addAttribute("friend", JSON.toJSON(friendlist));
        model.addAttribute("friendlist", arrayList);
        model.addAttribute("id", Long.valueOf(idStr));
        return "index";
    }

    @RequestMapping("/Chatpictures")
    @ResponseBody
    @CrossOrigin
    public Object Chatpictures(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedInputStream in = new BufferedInputStream(file.getInputStream());
        String originalFilename = file.getOriginalFilename();
        String path = ResourceUtils.getURL("classpath:template").getPath() + "/" + originalFilename.substring(originalFilename.lastIndexOf("\\") + 1);
        System.out.println(path);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(path));
        byte[] bytes = new byte[1024];
        int l;
        while ((l = in.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, l);
        }
        in.close();
        bufferedOutputStream.close();
        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        Map map = new HashMap<>();
        Map data = new HashMap();
        map.put("code", 0);
        map.put("msg", "无");
        data.put("src", request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "//template//" + originalFilename.substring(originalFilename.lastIndexOf("\\") + 1));
        map.put("data", data);
        response.addHeader("Access-Control-Allow-Origin", "*");
        System.out.println(JSON.toJSON(map));
        System.out.println("_________________");
        return JSON.toJSON(map);
    }

    private String getCookie(String name, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length == 0) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                return cookie.getValue();
            }
        }
        return null;
    }
}
